<template>
    <div>
      <el-card>
        <div class="header-container">
          <span class="header-title">用户管理</span>
         
        </div>
  
        <el-table :data="users" style="width: 100%" border>
          <el-table-column label="ID" prop="id"></el-table-column>
          <el-table-column label="用户名" prop="name"></el-table-column>
         
          <el-table-column label="电话" prop="phone"></el-table-column>
          <el-table-column label="性别" prop="sex"></el-table-column>
          <el-table-column label="地址" prop="address"></el-table-column>
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
                <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button type="text" @click="handleDelete(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
  
        <el-dialog :visible.sync="dialogVisible" title="添加用户">
          <el-form :model="newUser" label-width="80px">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="newUser.name"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="newUser.password"></el-input>
            </el-form-item>
            <el-form-item label="电话" prop="phone">
              <el-input v-model="newUser.phone"></el-input>
            </el-form-item>
            <el-form-item label="性别" prop="sex">
              <el-input v-model="newUser.sex"></el-input>
            </el-form-item>
            <el-form-item label="地址" prop="address">
              <el-input v-model="newUser.address"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="addUser">确认添加</el-button>
          </div>
        </el-dialog>
        <el-dialog :visible.sync="editDialogVisible" title="编辑用户">
        <el-form :model="editedUser" label-width="80px">
          <el-form-item label="用户名" prop="name">
            <el-input v-model="editedUser.name"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="editedUser.password"></el-input>
          </el-form-item>
          <el-form-item label="电话" prop="phone">
            <el-input v-model="editedUser.phone"></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-input v-model="editedUser.sex"></el-input>
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input v-model="editedUser.address"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="updateUser">确认编辑</el-button>
        </div>
      </el-dialog>
      </el-card>
    </div>
  </template>
  
  <script>
  import axios from 'axios';
  
  export default {
    data() {
      return {
        editDialogVisible: false,
        
        users: [],  // 用户数据包含更多信息
        newUser: {
          name: '',
          password: '',
          phone: '',
          sex: '',
          address: '',
        },
        editedUser: {
        id: '',
        name: '',
        password: '',
        phone: '',
        sex: '',
        address: '',
      },
        dialogVisible: false,
      };
    },
    created() {
      // 在组件创建时从后端获取用户列表
      this.fetchUsers();
    },
    methods: {
        handleEdit(user) {
      // 打开编辑用户的弹窗，并将用户信息填充到表单中
      this.editedUser = { ...user };
      this.editDialogVisible = true;
       // 编辑用户的逻辑
       this.$message.info(`编辑用户: ${user.name}`);
    },

    async updateUser() {
      try {
        await axios.put(`http://127.0.0.1:3000/api/users/${this.editedUser.id}`, this.editedUser);
        this.editDialogVisible = false;
        this.$message.success('用户信息更新成功');
        // 刷新用户列表
        this.fetchUsers();
      } catch (error) {
        console.error('Error updating user:', error);
      }
    },
   
      async fetchUsers() {
        try {
          const response = await axios.get('http://127.0.0.1:3000/api/users');
          this.users = response.data;
          this.users = this.users.slice(1);
        } catch (error) {
          console.error('Error fetching users:', error);
        }
      },
      showAddDialog() {
        this.dialogVisible = true;
      },
      async addUser() {
        try {
          const response = await axios.post('http://127.0.0.1:3000/api/users', this.newUser);
          this.dialogVisible = false;
          this.$message.success('用户添加成功');
          // 刷新用户列表
          this.fetchUsers();
        } catch (error) {
          console.error('Error adding user:', error);
        }
      },
      async updateUser() {
      try {
        await axios.put(`http://127.0.0.1:3000/api/users/${this.editedUser.id}`, this.editedUser);
        this.editDialogVisible = false;
        this.$message.success('用户信息更新成功');
        // 刷新用户列表
        this.fetchUsers();
      } catch (error) {
        console.error('Error updating user:', error);
      }
    },
  
      async handleDelete(userId) {
        try {
          await axios.delete(`http://127.0.0.1:3000/api/users/${userId}`);
          this.$message.success('用户删除成功');
          // 刷新用户列表
          this.fetchUsers();
        } catch (error) {
          console.error('Error deleting user:', error);
        }
      },
    },
  };
  </script>
  
  <style scoped>
  .header-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
  }
  
  .header-title {
    font-size: 18px;
    font-weight: bold;
  }
  
  .dialog-footer {
    display: flex;
    justify-content: flex-end;
  }
  </style>
  